package logic

import (
	"context"
	"fmt"
	"product/model/mysql"

	"product/goodsrpc/goodsrpc"
	"product/goodsrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *goodsrpc.LoginRequest) (*goodsrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Where("name=?", in.Name).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("没有该用户")
	}
	if user.Password != in.Password {
		return nil, fmt.Errorf("密码错误，请重新输入")
	}

	return &goodsrpc.LoginResponse{Id: int64(user.ID)}, nil
}
